package org.example.cake.controller;
import org.apache.ibatis.annotations.Param;
import org.example.cake.common.Rep;
import org.example.cake.entity.Userdiscount;
import org.example.cake.service.UserdiscountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/userdiscount")
public class UserdiscountController {

    @Autowired
    private UserdiscountService userdiscountService;


    /**
     * 根据用户id查询此用户有多少张优惠券
     * @param userid 用户id
     * @return 返回的个数
     */
    @GetMapping("/countByuserid")
    public int countByuserid(@RequestParam("userid") Integer userid){
        int countByuserid = userdiscountService.countByuserid(userid);
        System.out.println("countByuserid:"+countByuserid);
        if (countByuserid > 0){
            return countByuserid;
        }else {
            return 0;
        }
    }
    /**
     * 批量新增优惠券
     * @param userdiscounts 用户优惠券对象
     */
    @PostMapping("/batchInsertDiscount")
    public Rep batchInsertDiscount(@RequestBody List<Userdiscount> userdiscounts){
        Rep rep = new Rep();
        try {
            userdiscountService.batchInsertDiscount(userdiscounts);
            rep.setCode(200);
            rep.addData("结果","批量新增优惠券成功");
        }catch (Exception e){
            e.printStackTrace();
            rep.setCode(500);
            rep.addData("结果","批量新增优惠券失败！！！");
        }
        return rep;
    }

    @PostMapping("/getfestquanapi")
    public Rep getfestquanapi(@RequestBody Userdiscount userdiscount){
        //新增优惠券
        Integer res=userdiscountService.addfestOne(userdiscount);
        return new Rep().addData("res",res);
    }

}
